package com.lin.mr.maizilaundry.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.lin.mr.maizilaundry.R;

public class WebViewActivity extends Activity {
    private Button btn_webView;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        webView = (WebView) findViewById(R.id.webView);
        btn_webView = (Button) findViewById(R.id.btn_webView);
        //设置编码
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        //支持js
        webView.getSettings().setJavaScriptEnabled(true);
        //设置本地调用对象及其接口，绑定网页与控件
        webView.addJavascriptInterface(this,"myObj");
        //载入js
        webView.loadUrl("file:///android_asset/agreement.html");
        //点击调用js中的方法
        btn_webView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("javascript:clos()");
                Toast.makeText(WebViewActivity.this, "我是Android控件调用JavaScript方法", Toast.LENGTH_SHORT).show();
            }
        });
    }
    @JavascriptInterface
    //点击取消按钮时改变按钮文字
    public void fun1FromAndroid(String name){
        btn_webView.setText(name);
    }
}
